package org.ftc.service;

import java.util.Date;
import java.util.List;

import org.ftc.dao.MotionDao;
import org.ftc.domain.Motion;
import org.ftc.form.MotionForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

@Service
public class MotionService {

	@Autowired
	MotionDao motionDao;

	public Motion load(String id) {
		return motionDao.load(id);
	}

	public Motion get(String id) {
		return motionDao.get(id);
	}
	
	public List<Motion> getBySessionNo(String sessionNo) {
		return motionDao.getBySessionNo(sessionNo);
	}
	
	public Motion getByMotionNo(String motionNo) {
		return motionDao.getByMotionNo(motionNo);
	}

	public synchronized boolean save(Motion entity) {
		Authentication userDetails = SecurityContextHolder.getContext().getAuthentication();
		if (userDetails != null)
			entity.setCreationUser(userDetails.getName());
		entity.setCreationDate(new Date());
		return motionDao.save(entity);
	}

	public synchronized boolean saveOrUpdate(Motion entity) {
		return motionDao.saveOrUpdate(entity);
	}

	public synchronized boolean update(Motion entity) {
		entity.setModifyDate(new Date());
		entity.setModifyUser(SecurityContextHolder.getContext().getAuthentication().getName());
		return motionDao.update(entity);

	}

	public synchronized boolean delete(Motion entity) {
		return motionDao.delete(entity);
	}

	public synchronized boolean delete(String id) {
		return motionDao.delete(id);
	}

	public List<Motion> findPage(int offset, int pageSize) {
		return motionDao.findPage(offset, pageSize);
	}

	public List<Motion> findAll() {
		return motionDao.findAll();
	}

	public long countAll() {
		return motionDao.countAll();
	}

	public List<Motion> findPage(MotionForm form, int offset, int pageSize) {
		return motionDao.findPage(form, offset, pageSize);
	}
	
	public List<Motion> findPage2(MotionForm form, int offset, int pageSize) {
		return motionDao.findPage2(form, offset, pageSize);
	}

	public long count(MotionForm form) {
		return motionDao.count(form);
	}
	
	public List<Motion> findAll(MotionForm form) {
		return motionDao.findAll(form);
	}
	
}
